﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FTools.Interface;
using FTools.Persistencia;
using System.Data.SqlClient;
using FTools.Dominio;

namespace FTools.Comunicacion
{
    class ComSpStock : InterfaceSpStock
    {
        Conexion objConexion = new Conexion();
        SqlConnection conexion;
        SqlTransaction transaccion = null;
        PerSpStock objPerSpStock = new PerSpStock();

        public SpStock obtenerSpStock(long xIdSubProducto)
        {
            try
            {
                conexion = objConexion.abrirConexion();
                transaccion = conexion.BeginTransaction();
                SpStock objSpStock = objPerSpStock.obtenerSpStock(conexion, transaccion, xIdSubProducto);
                transaccion.Commit();
                return objSpStock;
            }
            catch (Exception ex)
            {
                throw new Exception("#Comunicacion# " + ex.Message);
            }
            finally
            {
                objConexion.cerrarConexion();
            }
        }

        public Boolean existeSpStock(long xIdSubProducto)
        {
            try
            {
                conexion = objConexion.abrirConexion();
                transaccion = conexion.BeginTransaction();
                Boolean existe = objPerSpStock.existeSpStock(conexion, transaccion, xIdSubProducto);
                transaccion.Commit();
                return existe;
            }
            catch (Exception ex)
            {
                throw new Exception("#Comunicacion# " + ex.Message);
            }
            finally
            {
                objConexion.cerrarConexion();
            }
        }

        public int altaSpStock(SpStock xObjSpStock)
        {
            try
            {
                conexion = objConexion.abrirConexion();
                transaccion = conexion.BeginTransaction();
                int nro = objPerSpStock.altaSpStock(conexion, transaccion, xObjSpStock);
                transaccion.Commit();
                return nro;
            }
            catch (Exception ex)
            {
                transaccion.Rollback();
                throw new Exception("#Comunicacion# " + ex.Message);
            }
            finally
            {
                objConexion.cerrarConexion();
            }
        }

        public int bajaSpStock(SpStock xObjSpStock)
        {
            try
            {
                conexion = objConexion.abrirConexion();
                transaccion = conexion.BeginTransaction();
                int nro = objPerSpStock.bajaSpStock(conexion, transaccion, xObjSpStock);
                transaccion.Commit();
                return nro;
            }
            catch (Exception ex)
            {
                transaccion.Rollback();
                throw new Exception("#Comunicacion# " + ex.Message);
            }
            finally
            {
                objConexion.cerrarConexion();
            }
        }

        public int modificaSpStock(SpStock xObjSpStock)
        {
            try
            {
                conexion = objConexion.abrirConexion();
                transaccion = conexion.BeginTransaction();
                int nro = objPerSpStock.modificaSpStock(conexion, transaccion, xObjSpStock);
                transaccion.Commit();
                return nro;
            }
            catch (Exception ex)
            {
                transaccion.Rollback();
                throw new Exception("#Comunicacion# " + ex.Message);
            }
            finally
            {
                objConexion.cerrarConexion();
            }
        }
    }
}
